package com.qifeng.modules.postInfo.controller;

import com.qifeng.common.base.RestResponse;
import com.qifeng.modules.postInfo.domain.PostInfo;
import com.qifeng.modules.postInfo.service.PostInfoService;
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "postInfo")
@RequestMapping("api")
public class PostInfoController {

    private static Logger logger = Logger.getLogger(PostInfoController.class);

    @Autowired
    private PostInfoService postInfoService;

    /**
     * 获取列表
     *
     * @param response
     * @return
     */
    @ApiOperation(value = "分页查询所有", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
    })
    @GetMapping("postInfos")
    public RestResponse<Page<PostInfo>> findPage(Pageable pageable, RestResponse response) {
        return response.success(postInfoService.findPage(pageable));
    }

    /**
     * 新增
     *
     * @param postInfo 新增实体，前端数据需要 JSON.stringify(data)
     * @param response
     * @return
     */
    @ApiOperation(value = "新增", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @PostMapping("postInfo")
    public RestResponse create(@ApiParam(value = "实体", required = true)
                               @RequestBody PostInfo postInfo,
                               RestResponse response) {
        return response.success(postInfoService.create(postInfo));
    }

    /**
     * 更新
     *
     * @param postInfo 更新实体，前端数据需要 JSON.stringify(data)
     * @param response
     * @return
     */
    @ApiOperation(value = "修改", notes = "")
    @ApiResponses({
            @ApiResponse(code = 201, message = "修改成功"),
            @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @PatchMapping("postInfo")
    public RestResponse update(@ApiParam(value = "实体", required = true)
                               @RequestBody PostInfo postInfo,
                               RestResponse response) {
        return response.success(postInfoService.update(postInfo));
    }


    /**
     * 删除
     *
     * @param id       主键
     * @param response
     * @return
     */
    @ApiOperation(value = "删除", notes = "")
    @ApiResponses({
            @ApiResponse(code = 201, message = "修改成功"),
            @ApiResponse(code = 400, message = "参数验证失败", response = RestResponse.class)
    })
    @DeleteMapping("postInfo")
    public RestResponse delete(@ApiParam(value = "主键", required = true)
                               @RequestParam Long id,
                               RestResponse response) {
        postInfoService.delete(postInfoService.findOne(id));
        return response.success();
    }

    /**
     * 根据ID查找
     *
     * @param id       主键
     * @param response
     * @return
     */
    @ApiOperation(value = "通过主键查找", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
    })
    @GetMapping("postInfo")
    public RestResponse<PostInfo> findOne(@ApiParam(value = "主键", required = true)
                                          @RequestParam Long id,
                                          RestResponse response) {
        return response.success(postInfoService.findOne(id));
    }

    /**
     * 根据User_ID查找
     *
     * @param id       用户ID
     * @param response
     * @return
     */
    @ApiOperation(value = "查找当前用户发布的订单", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
    })
    @GetMapping("postInfo/user")
    public RestResponse<PostInfo> findByUserId(@ApiParam(value = "用户ID", required = true)
                                               @RequestParam Long id,
                                               RestResponse response) {
        return response.success(postInfoService.findByUserId(id));
    }


    /**
     * 订单状态查询
     *
     * @param state
     * @param response
     * @return
     */
    @ApiOperation(value = "查找当前用户发布的订单 1:待审核 2:待完成(state = 1时时间未过期,state = 2时间过期未评价)", notes = "")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 404, message = "查询失败", response = RestResponse.class)
    })
    @GetMapping("postInfo/status")
    public RestResponse<?> findByState(@ApiParam(value = "状态", required = true)
                                       @RequestParam Long state,
                                       RestResponse response) {
        return response.success(postInfoService.findByState(state));
    }

}